package xxy.com.yangproject.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.ToastUtils;
import com.shizhefei.view.indicator.Indicator;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.DrawableBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;

import xxy.com.yangproject.R;

public class NavigationFragment extends Fragment {
    private LayoutInflater inflate;
    private FragmentActivity mContext;
    private IndicatorViewPager.IndicatorFragmentPagerAdapter indicatorViewPagerAdapter;
    private NavController navController;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.frag_navigation, container, false);
        ViewDataBinding inflate = DataBindingUtil.inflate(inflater, R.layout.frag_navigation, container, false);
        mContext = getActivity();
        return inflate.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        Bundle arguments = getArguments();
//        if (arguments != null) {
//            String testData = arguments.getString("testData");
//            TextView textView = view.findViewById(R.id.tv_navigation_sec);
//            textView.setText(testData);
//        }
        navController = Navigation.findNavController(view);
        //indicator 和 viewPager
        initIndicatorFragmentPagerAdapter();
        ScrollIndicatorView scrollIndicatorView = view.findViewById(R.id.ScrollIndicatorView);
        ViewPager viewPager = view.findViewById(R.id.ViewPager);
        inflate = LayoutInflater.from(mContext);
        IndicatorViewPager indicatorViewPager = new IndicatorViewPager(scrollIndicatorView, viewPager);
        indicatorViewPager.setAdapter(indicatorViewPagerAdapter);
        indicatorViewPager.setIndicatorScrollBar(new DrawableBar(getActivity(),
                R.drawable.class_indicator));   //设置指示滑块的滑动块样式  OnTransitionTextListener
        scrollIndicatorView.setOnTransitionListener(new OnTransitionTextListener() {

            @Override
            public TextView getTextView(View tabItemView, int position) {
                return tabItemView.findViewById(R.id.TabText);
            }

            @Override
            public void onTransition(View view, int position, float selectPercent) {
                super.onTransition(view, position, selectPercent);
                final AppCompatImageView imageView = view.findViewById(R.id.TabFlag);
                imageView.setAlpha(1 - selectPercent);   //消失或者出现
            }
        });

        scrollIndicatorView.setOnIndicatorItemClickListener(new Indicator.OnIndicatorItemClickListener() {
            @Override
            public boolean onItemClick(View clickItemView, int position) {
                ToastUtils.showShort((position+ 1) + "");
                return false;
            }
        });
        //跳转到 NavigationThirdFragment 页面
        view.findViewById(R.id.bt_goto_third_fragment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navController.navigate(R.id.action_sec_navigation_to_third_navigation);
            }
        });
    }

    //初始化Indicator FragmentPager Adapter
    private void initIndicatorFragmentPagerAdapter() {
        indicatorViewPagerAdapter = new IndicatorViewPager.IndicatorFragmentPagerAdapter(getChildFragmentManager()) {
            private int[] images = { R.drawable.backgroud, R.drawable.bg_board, R.drawable.ic_head,
                    R.drawable.pop_update_bg };
            @Override
            public int getCount() {
                return images.length;
            }

            //tab
            @Override
            public View getViewForTab(int position, View convertView, ViewGroup container) {
                if (convertView == null) {
                    convertView = inflate.inflate(R.layout.table_item_layout2, container, false);
                }
                ((AppCompatTextView) convertView.findViewById(R.id.TabText))
                        .setText("第" + position + "页");
//                convertView.setBackgroundColor(R.color.colorPrimary);
//                convertView.setTag("1");

                String tag = (String)convertView.getTag();

                //调用时机不对，视图还没创建完，(convertView.findViewById(R.id.TabFlag).getWidth() 等于0
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(convertView.getLayoutParams().width
//                        , (convertView.getLayoutParams().height));
//
//                layoutParams.setMarginStart(((convertView.findViewById(R.id.TabFlag)).getWidth()) *40);
//                LogUtils.eTag(Constant.TAG,  "Tab标签的宽度 : "+(((AppCompatImageView)convertView.findViewById(R.id.TabFlag)).getMeasuredWidth()) +"");
//                convertView.setLayoutParams(layoutParams);


//                if (0 == position) {
//                    convertView.setLayoutParams(new FrameLayout.LayoutParams((int)
//                            (getResources().getDisplayMetrics().density * 90)
//                            , (int) (getResources().getDisplayMetrics().density * 35)));
//                } else if (10 > position) {
//                    convertView.setLayoutParams(new FrameLayout.LayoutParams((int)
//                            (getResources().getDisplayMetrics().density * 70)
//                            , (int) (getResources().getDisplayMetrics().density * 35)));
//                }
                return convertView;
            }

            @Override
            public Fragment getFragmentForPage(int position) {
                return NavigationChildFragment.buildFragment(position);
            }
        };
    }


}
